package com.example.idachuappone.promptly.item;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.promptly.entity.Addresses;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.CusProSmallBgDialog;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class SelectAddressActivity extends BaseActivity implements OnClickListener {
	private Addresses address;
	private Button btn_back;
	private Button btn_index;
	private Button btn_set_default;
	private CusProSmallBgDialog dialog;
	private int position;
	private TextView tv_address_dizi;
	private TextView tv_address_xiaoqu;
	private TextView tv_name;
	private TextView tv_phone;
	private int updateRequest;

	public SelectAddressActivity() {
		this.updateRequest = 255;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_address);
		this.dialog = new CusProSmallBgDialog(this);
		this.address = (Addresses) getIntent().getExtras().getSerializable("address");
		this.position = getIntent().getExtras().getInt("position");
		initView();
		setData(this.address);
	}

	private void setData(Addresses addresses) {
		this.tv_name.setText(addresses.getName());
		this.tv_phone.setText(addresses.getPhone());
		this.tv_address_xiaoqu.setText(addresses.getCommunity());
		this.tv_address_dizi.setText(addresses.getDoor_number());
	}

	private void initView() {
		this.tv_name = (TextView) findViewById(R.id.tv_name);
		this.tv_phone = (TextView) findViewById(R.id.tv_phone);
		this.tv_address_xiaoqu = (TextView) findViewById(R.id.tv_address_xiaoqu);
		this.tv_address_dizi = (TextView) findViewById(R.id.tv_address_dizi);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.btn_index = (Button) findViewById(R.id.btn_index);
		this.btn_index.setOnClickListener(this);
		this.btn_set_default = (Button) findViewById(R.id.btn_set_default);
		this.btn_set_default.setOnClickListener(this);
		if (this.position == 0) {
			this.btn_set_default.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		} else {
			this.btn_set_default.setVisibility(0);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.btn_index /* 2131165213 */:
			Intent intent = new Intent(this, UpdateAddressActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("address", this.address);
			bundle.putInt("position", this.position);
			intent.putExtras(bundle);
			startActivityForResult(intent, this.updateRequest);
		case R.id.btn_set_default /* 2131165413 */:
			Map<String, String> map = new HashMap();
			map.put("id", this.address.getId());
			NetUtil.post(this, Constant.SET_ADDRESSS, map, new RequestCallBack<String>() {
				@Override
				public void onStart() {
					super.onStart();
					SelectAddressActivity.this.dialog
							.setMsg("\u8bbe\u7f6e\u9ed8\u8ba4\u670d\u52a1\u5730\u5740\u4e2d...");
					SelectAddressActivity.this.dialog.show();
				}

				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					SelectAddressActivity.this.dialog.dismiss();
					MainToast.show(SelectAddressActivity.this, "\u9ed8\u8ba4\u5730\u5740\u8bbe\u7f6e\u6210\u529f", 0);
					AppShareData.isRefreshAddress = true;
					AppShareData.isRefreshAddressChoose = true;
					SelectAddressActivity.this.finish();
				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					SelectAddressActivity.this.dialog.dismiss();
					MainToast.show(SelectAddressActivity.this,
							SelectAddressActivity.this.getResources().getString(R.string.netFail), 0);
				}
			});
		default:
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent data) {
		super.onActivityResult(arg0, arg1, data);
		if (arg0 == this.updateRequest && arg1 == -1) {
			AppShareData.isRefreshAddress = true;
			AppShareData.isRefreshAddressChoose = true;
			updateAddress();
		}
	}

	private void updateAddress() {
		NetUtil.get(this, new StringBuilder(Constant.SELECT_ADDRESS_ID).append(this.address.getId()).toString(), 100,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						super.onStart();
						SelectAddressActivity.this.dialog.setMsg("\u52a0\u8f7d\u4e2d...");
						SelectAddressActivity.this.dialog.show();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						SelectAddressActivity.this.dialog.dismiss();
						try {
							JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
							if (jsonObject.getInt("code") == 10000) {
								Addresses addresses = new Addresses();
								addresses.parseJson(jsonObject.getJSONObject("data"));
								SelectAddressActivity.this.address = addresses;
								SelectAddressActivity.this.setData(addresses);
								return;
							}
							MainToast.show(SelectAddressActivity.this, jsonObject.getString("error"), 0);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						SelectAddressActivity.this.dialog.dismiss();
						MainToast.show(SelectAddressActivity.this,
								SelectAddressActivity.this.getResources().getString(R.string.netFail), 0);
					}
				});
	}
}
